package kr.or.kead.ui.input;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.KeyboardFocusManager;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.regex.Pattern;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import kr.or.kead.domain.Employee;
import kr.or.kead.service.DaoEmp;



@SuppressWarnings("serial")
public class EmpManagerment extends AbsInput implements ActionListener {	
	private JTextField txt_code;
	private JTextField txt_name;
	private DaoEmp daoEmp;
	private Employee emp;
	
	
	
	private JComboBox<String> combo_level;

	public EmpManagerment(JFrame frame) {
		this.frame = frame;
		daoEmp = new DaoEmp();
		emp = new Employee();
		initialize();
	}
	
	

	protected void create_north_panel() {		
		northPanel = new JPanel();
		
		
		
		northPanel.setLayout(new GridLayout(3, 2, 10, 10));
		
		JLabel lbl_code = new JLabel("사원코드");
		lbl_code.setFont(font);
		northPanel.add(lbl_code);
		
		txt_code = new JTextField();
		txt_code.setFont(font);
		
		northPanel.add(txt_code);
		txt_code.setColumns(4);
		txt_code.addKeyListener(this);
		
		JLabel lbl_name = new JLabel("사  원  명");
		lbl_name.setFont(font);
		northPanel.add(lbl_name);
		
		txt_name = new JTextField();
		txt_name.setFont(font);
		northPanel.add(txt_name);
		txt_name.setColumns(9);
		txt_name.addKeyListener(this);
		
		
		
		JLabel lbl_level = new JLabel("사원등급");
		lbl_level.setFont(font);
		northPanel.add(lbl_level);
		

	      
		
		combo_level = new JComboBox();
		combo_level.setMaximumRowCount(3);
		combo_level.setEditable(false);
		combo_level.setEditable(true);
		combo_level.setModel(new DefaultComboBoxModel(new String[] {"등급선택", "A", "B", "C"}));
		combo_level.setSelectedIndex(0);
		combo_level.addActionListener(this);		
		northPanel.add(combo_level);
		
	}
	

	protected void create_south_panel() {
		southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		
		
		
		btn_save = new JButton("저장");
		btn_save.addActionListener(this);
		btn_save.setFont(font);
		southPanel.add(btn_save);
		
		btn_del = new JButton("삭제");
		btn_del.addActionListener(this);
		btn_del.setFont(font);
		southPanel.add(btn_del);
		
		btn_search = new JButton("검색");
		btn_search.addActionListener(this);
		btn_search.setFont(font);
		southPanel.add(btn_search);
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "저장") {
			regular_expressin();
			emp.setE_code(txt_code.getText());
			emp.setE_name(txt_name.getText());
			emp.setE_level((String) combo_level.getSelectedItem());
			if(daoEmp.insertDao(emp) == 0) {
				JOptionPane.showMessageDialog(btn_save, "저장 되었습니다.");
				clear_component();
			}else {
				JOptionPane.showMessageDialog(btn_save, "사원코드가 중복되었습니다.");
			}
			
		}else if(e.getActionCommand() == "삭제") {
			if(daoEmp.deleteDao(txt_code.getText()) == 0) {
				JOptionPane.showMessageDialog(btn_del, "삭제 되었습니다.");
				clear_component();
			}else {
				JOptionPane.showMessageDialog(btn_del, "해당코드는 존재하지 않습니다.");
			}
		}else if(e.getActionCommand() == "검색") {
			emp = (Employee)daoEmp.selectDao(txt_code.getText());
			if(emp !=null ) {				
				txt_code.setText(emp.getE_code());
				txt_name.setText(emp.getE_name());
				combo_level.setSelectedItem(emp.getE_level());				
			}else {
				JOptionPane.showMessageDialog(btn_del, "해당코드는 존재하지 않습니다.");
			}
		}
		
	}
	
	public int regular_expressin() {
		
		if(Pattern.matches("^[a-z, A-Z]{1}[a-z,A-Z,0-9]{3}$", txt_code.getText()) == false) {
			JOptionPane.showMessageDialog(btn_save, "사원코드를 잘못 입력하셨습니다.");
			return -1;
		}else if(Pattern.matches("^[a-z, A-Z]{1,9}||[가-힣]{1,4}$", txt_name.getText()) == false) {
			JOptionPane.showMessageDialog(btn_save, "사원이름을 잘못 입력하셨습니다.");
			return -1;
		}else if(combo_level.getSelectedItem() == "등급선택") {
			JOptionPane.showMessageDialog(btn_save, "사원등급을 선택하여 주십시오.");
			return -1;
		}
		
		return 0;
	}
	
	public void clear_component() {
		txt_code.setText("");
		txt_name.setText("");
		combo_level.setSelectedIndex(0);
	}






	
}
